<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Models\Buildings;
use App\Models\Areas;
use App\Models\BuildType;
use App\Http\Requests\BuildUpdateRequest;
use Log;
use App\Models\Build\Detailed as Detailed;
class BuildController extends Controller
{
    protected $fields = [
        'name' => '',
        'info' => '',
        'username' =>'',
        'phone' =>'',
        'attach_id'=>'',
        'prize'=>'',
        'area_id'=>'',
        'type_id'=>'',
        'logo'=>'',
        'long'=>'',
        'lat'=>'',
        'vote_count'=>'',
        'location'=>'',
    ];

    protected $d_fields = [
        'build_id'=>'', 'word'=>'', 'word_wordkey'=>'','word_text'=>'','count'=>'','year_time_three_name'=>'','year_time_two_name'=>'','year_time_one_name'=>'',
        'count_wordkey'=>'','count_text'=>'','year'=>'','year_wordkey'=>'','year_time_one'=>'','year_time_two'=>'','year_time_three'=>'',
        'personae'=>'','personae_wordkey'=>'','personae_text'=>'','team'=>'','team_wordkey'=>'','team_text'=>'','end'=>'','end_text'=>'',
        'word_img'=>'','personae_img'=>'','count_img'=>'','team_img'=>'','team_img'=>'','end_img'=>''
    ];

    public function index(Request $request){
        if($request->ajax()){
            $data = array();
            $data['draw'] = $request->get('draw');
            $start = $request->get('start');
            $length = $request->get('length');
            $order = $request->get('order');
            $columns = $request->get('columns');
            $search = $request->get('search');
            $name =$request->get('name');
            $data['recordsTotal'] = Buildings::count();
            if (strlen($search['value']) > 0) {
                $data['recordsFiltered'] = Buildings::where(function ($query) use ($search) {
                    $query->where('name', 'LIKE', '%' . $search['value'] . '%')
                    ->orWhere('phone', '=', $search['value']);
                })->count();
            
                $data['data'] = Buildings::with('areas')->where(function ($query) use ($search) {
                    $query->where('name', 'LIKE', '%' . $search['value'] . '%')
                    ->orWhere('phone', '=', $search['value']);
                })
                ->skip($start)->take($length)
                ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                ->get();
            
            }else{
                $data['recordsFiltered'] = Buildings::count();
                $data['data'] = Buildings::with('areas')->
                skip($start)->take($length)
                ->orderBy($columns[$order[0]['column']]['data'], $order[0]['dir'])
                ->get();
            
            }
            
            return response()->json($data);
        }
        return view("admin.build.index");
    }
    
    public function show($id){
        
    }
    
    public function create(){
        $data = [];
        foreach ($this->fields as $field => $default) {
            $data[$field] = old($field, $default);
        }
        $data['areasAll'] = Areas::all()->toArray();
        $data['typeAll'] = BuildType::all()->toArray();
        $data['imgUrl']= "";
        return view('admin.build.create', $data);
//         return view('admin.build.edit');
    }
    
    public function store(Request $request){

        $Build= new Buildings();
        foreach (array_keys($this->fields) as $field) {
            $Build->$field = $request->get($field);
        }
        unset($Build->perms);
        if ($Build->save()){
            $id = $Build->id;
            return redirect('/admin/build/create/detail?id='.$id)->withSuccess('添加成功！');
        }

    }

    public function createDetail(Request $request){
        $id = $request->get('id');
        $build = Buildings::find($id);
        if($build){
            $data = [];
            foreach ($this->d_fields as $d_fields => $default) {
                $data[$d_fields] = old($d_fields, $default);
            }
            $data['build_id']=$id;
            return view('admin.build.create.detail',$data);
        }else{
            return redirect('/admin/build')->withSuccess('添加失败!');
        }

    }

    public function storeDetail(Request $request){

        $Detailed= new Detailed();

        foreach (array_keys($this->d_fields) as $d_fields) {
            $Detailed->$d_fields= $request->get($d_fields);
        }
        unset($Detailed->perms);
        if ($Detailed->save()){
            return redirect('/admin/build')->withSuccess('添加成功！');
        }

    }
    
     public function edit($id)
        {

            $Build = Buildings::find((int)$id);
            if (!$Build) return redirect('/admin/bulid')->withErrors("找不到该楼盘!");

            foreach (array_keys($this->fields) as $field) {
                $data[$field] = old($field, $Build->$field);
            }
            $data['imgUrl']= $Build->attach->path;
            $data['areasAll'] = Areas::all()->toArray();
            $data['typeAll'] = BuildType::all()->toArray();

            $data['id'] = (int)$id;
            return view('admin.build.edit', $data);
        }
    
    
       public function update(BuildUpdateRequest $request, $id)
        {
            $Build = Buildings::find((int)$id);
            foreach (array_keys($this->fields) as $field) {
                $Build->$field = $request->get($field);
            }
            if($Build->save()){
                return redirect('/admin/build/edit/detail?id='.$id)->withSuccess('修改成功！');
            }

//            return redirect('/admin/build')->withSuccess('修改成功！');
        }


    public function editDetail(Request $request)
    {
        $id= $request->id;
        $Build = Buildings::find((int)$id);
        if (!$Build) return redirect('/admin/bulid')->withErrors("找不到该楼盘!");

        $Detailed =Detailed::where('build_id','=',$id)->first();
        if(!$Detailed) return redirect('/admin/build/create/detail?id='.$id)->withSuccess('修改成功！');


        foreach (array_keys($this->d_fields) as $d_fields) {
            $data[$d_fields] = old($d_fields, $Detailed->$d_fields);
        }
        $data['id']=$Detailed->id;
        return view('admin.build.edit.detail',$data);
    }

    public function updateDetail(Request $request, $id)
    {

        $Detailed = Detailed::find((int)$id);
        foreach (array_keys($this->d_fields) as $d_fields) {
            $Detailed->$d_fields = $request->get($d_fields);
        }
        $Detailed->save();
         return redirect('/admin/build')->withSuccess('修改成功！');
    }
        
    public function destroy($id){

            $Build = Buildings::find((int)$id);
            if ($Build) {
                $Detailed =Detailed::where('build_id','=',$id)->first();
                $Detailed->delete();
                $Build->delete();
            } else {
                return redirect()->back()
                    ->withErrors("删除失败");
            }
            return redirect()->back()
                ->withSuccess("删除成功");
        }
        
}
